package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.dao.SaveCCMapper;
import com.example.demo.entity.PictureCc;
import com.example.demo.entity.SaveCC;
import com.example.demo.entity.VideoCc;
import com.example.demo.entity.VoiceCc;
import com.example.demo.service.*;
import com.example.demo.util.CosUtil;
import com.example.demo.vo.DataView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class QRCodeReceiverOrSendServiceImpl extends ServiceImpl<SaveCCMapper, SaveCC> implements QRCodeReceiverService {

    @Value("${qrCode.voices}")
    private String voicePath;

    @Value("${qrCode.videos}")
    private String videoPath;

    @Value("${qrCode.images}")
    private String imagesPath;

    @Autowired
    private PictureCcService pictureCcService;

    @Autowired
    private VoiceCcService voiceCcService;

    @Autowired
    private VideoCcService videoCcService;

    @Autowired
    private SaveCCService saveCCService;


    @Override
    public DataView receiveImage(MultipartFile[] images, HttpServletRequest request) throws IOException {
        DataView dataView = new DataView();

        String qrcode = request.getHeader("qrcode");
        if(qrcode == null)
        {
            dataView.setCode(300);
            dataView.setMsg("请扫描二维码");
            return dataView;
        }

        List<PictureCc> list = new ArrayList<>();


        for (MultipartFile image :images)
        {
            String pathFile = imagesPath+qrcode+"/"+image.getOriginalFilename();

            String path = CosUtil.uploadResource(image, pathFile);

            PictureCc pictureCc = new PictureCc();
            pictureCc.setQrCodeId(qrcode);
            pictureCc.setPicturePath(path);
            list.add(pictureCc);
        }

        boolean b = pictureCcService.saveBatch(list);

        dataView.setMsg("图片上传成功");
        dataView.setCode(200);
        return dataView;
    }

    @Override
    public DataView receiveVideo(MultipartFile[] videos, HttpServletRequest request) throws IOException {

        DataView dataView = new DataView();

        String qrcode = request.getHeader("qrcode");
        if (qrcode == null)
        {
            dataView.setCode(300);
            dataView.setMsg("请扫描二维码");
            return dataView;
        }

        List<VideoCc> list = new ArrayList<>();
        for (MultipartFile video : videos)
        {
            String pathFile = videoPath+qrcode+"/"+video.getOriginalFilename();
            String path = CosUtil.uploadResource(video, pathFile);

            VideoCc videoCc = new VideoCc();
            videoCc.setVideoPath(path);
            videoCc.setQrCodeId(qrcode);
            list.add(videoCc);
        }

        videoCcService.saveBatch(list);

        dataView.setMsg("视频上传成功");
        dataView.setCode(200);
        return dataView;
    }

    @Override
    public DataView recevieVoice(MultipartFile voice, HttpServletRequest request) throws IOException {
        DataView dataView = new DataView();

        String qrcode = request.getHeader("qrcode");
        if (qrcode == null)
        {
            dataView.setCode(300);
            dataView.setMsg("请扫描二维码");
            return dataView;
        }

        String pathFile = voicePath+qrcode+"/"+voice.getOriginalFilename();

        String path = CosUtil.uploadResource(voice, pathFile);

        VoiceCc voiceCc = new VoiceCc();
        voiceCc.setVoicePath(path);
        voiceCc.setQrCodeId(qrcode);
        voiceCcService.save(voiceCc);
        return dataView;
    }

    @Override
    public DataView send(HttpServletRequest request) {
        DataView dataView = new DataView();

        String qrcode = request.getHeader("qrcode");
        if(qrcode == null)
        {
            dataView.setCode(300);
            dataView.setMsg("请扫描二维码");
            return dataView;
        }

        SaveCC saveCC = saveCCService.query().eq("qr_code_id", qrcode).one();
        if (saveCC == null)
        {
            return dataView;
        }

        List<PictureCc> pictureCcs = pictureCcService.query().eq("qr_code_id", qrcode).list();
        List<VideoCc> videoCcs = videoCcService.query().eq("qr_code_id", qrcode).list();
        VoiceCc voiceCc = voiceCcService.query().eq("qr_code_id", qrcode).one();
        List<String> picturePaths = pictureCcs.stream().map((pictureCc -> {
            return pictureCc.getPicturePath();
        })).collect(Collectors.toList());
        saveCC.setPicturePaths(picturePaths);

        List<String> videoPaths = videoCcs.stream().map(videoCc -> {
            return videoCc.getVideoPath();
        }).collect(Collectors.toList());
        saveCC.setVideoPaths(videoPaths);

        if(voiceCc!= null)
        {
            saveCC.setVoicePath(voiceCc.getVoicePath());
        }
        dataView.setData(saveCC);
        dataView.setCode(200);
        return dataView;
    }
}
